/*
 * Copyright 2009-2015 by WWW.KNET.CN  All rights reserved.
 */
package cn.knet.sp.ncrm.modules.sys.customertype.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.sys.customertype.entity.CustomerType;
import cn.knet.sp.ncrm.modules.sys.customertype.service.CustomerTypeService;
import cn.knet.sp.ncrm.modules.sys.utils.CustomerUtils;


/**
 * 客户类型
 *
 * @author zhangbin
 * @version 2015 /08/27 10:50:36
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/customerType")
public class CustomerTypeController extends BaseController {

	@Autowired
	private CustomerTypeService customerTypeService;

	@ModelAttribute
	public CustomerType get(@RequestParam(required = false) String id) {
		CustomerType entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = customerTypeService.get(id);
		}
		if (entity == null) {
			entity = new CustomerType();
		}
		return entity;
	}

	private void trim(CustomerType customerType) {
		if(customerType!=null&&StringUtils.isNotBlank(customerType.getName())){
			customerType.setName(StringUtils.trim(customerType.getName()));
		}
	}

	@RequiresPermissions("sys:customerType:view")
	@RequestMapping(value = {"list", ""})
	public String list(CustomerType customerType, HttpServletRequest request, HttpServletResponse response,
					   Model model) {
		return "modules/sys/customertype/customerTypeAjaxList";
	}

	@RequiresPermissions("sys:customerType:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(CustomerType customerType, HttpServletRequest request, HttpServletResponse response,
							 Model model) {
		Page<CustomerType> page = customerTypeService.findPage(new Page<CustomerType>(request, response), customerType);
		return page;
	}

	@RequiresPermissions("sys:customerType:view")
	@RequestMapping(value = "form")
	public String form(CustomerType customerType, Model model) {
		customerType.addHistoryFlag();
		model.addAttribute("customerType", customerType);
		return "modules/sys/customertype/customerTypeForm";
	}

	@RequiresPermissions("sys:customerType:edit")
	@RequestMapping(value = "save")
	public String save(CustomerType customerType, Model model, RedirectAttributes redirectAttributes) {
		this.trim(customerType);
		if (!beanValidator(model, customerType)) {
			return form(customerType, model);
		}
		if(customerType.getIsNewRecord()){
			customerType.setDateCreated(new java.util.Date());
		}
		int count = customerTypeService.findDistinctCustomerType(customerType);
		if(count>0){
			addMessage(model, "类型["+customerType.getName()+"]已经存在,请更换名称");
			return form(customerType, model);
		}
		customerType.setLastUpdated(new java.util.Date());
		customerTypeService.save(customerType);
		CustomerUtils.clearType(customerType);//清除缓存
		addMessage(redirectAttributes, "保存客户类型成功");
		return "redirect:" + Global.getAdminPath() + "/sys/customerType/?repage";
	}

	@RequiresPermissions("sys:customerType:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(CustomerType customerType) {
		AjaxMessage result = AjaxMessage.success();
		try {
			customerTypeService.delete(customerType);
			CustomerUtils.clearType(customerType);
			result.appendMessage("删除客户类型成功!");
		} catch (Exception e) {
			logger.warn("删除客户类型失败", e);
			result.makeError("删除客户类型失败!系统发生异常");
		}
		return result;
	}

}